/************************************************************************************/
/*  Program name CAPGLIMP_JE_BATCHES.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_JE_BATCHES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_JE_BATCHES;
	
	lv_Next_Seq_ID		NUMBER;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	GL_JE_BATCHES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_GL_JE_BATCHES_MAP	VALUES
			(
			C1.JE_BATCH_ID,
			C1.SET_OF_BOOKS_ID,
			C1.NAME,
			C1.DEFAULT_PERIOD_NAME,
			lv_Next_Seq_ID,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			C1.NAME,
			C1.DEFAULT_PERIOD_NAME
			);
			
			INSERT INTO GL_JE_BATCHES
			VALUES
			(
			lv_Next_Seq_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			C1.NAME,
			C1.STATUS,
			C1.STATUS_VERIFIED,
			C1.ACTUAL_FLAG,
			C1.DEFAULT_EFFECTIVE_DATE,
			C1.AVERAGE_JOURNAL_FLAG,
			C1.BUDGETARY_CONTROL_STATUS,
			C1.APPROVAL_STATUS_CODE,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.STATUS_RESET_FLAG,
			C1.DEFAULT_PERIOD_NAME,
			C1.UNIQUE_DATE,
			C1.EARLIEST_POSTABLE_DATE,
			C1.POSTED_DATE,
			C1.DATE_CREATED,
			C1.DESCRIPTION,
			C1.CONTROL_TOTAL,
			C1.RUNNING_TOTAL_DR,
			C1.RUNNING_TOTAL_CR,
			C1.RUNNING_TOTAL_ACCOUNTED_DR,
			C1.RUNNING_TOTAL_ACCOUNTED_CR,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.PARENT_JE_BATCH_ID),
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.CONTEXT,
			C1.UNRESERVATION_PACKET_ID,
			C1.PACKET_ID,
			C1.USSGL_TRANSACTION_CODE,
			C1.CONTEXT2,
			C1.POSTING_RUN_ID,
			C1.REQUEST_ID,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	UPDATE
		GL_JE_BATCHES
	SET	
		PARENT_JE_BATCH_ID	=	CAPBRIMP_MAPPING.GetJEBatchIDMapping(PARENT_JE_BATCH_ID)
	WHERE
		PARENT_JE_BATCH_ID IS NOT NULL
	AND	JE_BATCH_ID	IN
		(
		SELECT	JE_BATCH_ID
		FROM	CRST_GL_JE_BATCHES
		);
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/